<script type="text/javascript">
    $(function () {
        var khdatas = [];
        $.ajax({
            url:$.webPath() + "/organ/qyjg/findAllKhByNbgsId?nbgsId=${gsId!}",
            async:false,
            dataType:'json',
            success:function (data) {
                khdatas = data;
                khdatas.push({id:-1,jc:'库存零售'});
            }
        });
        $("#hkjhmx table[name='datagrid']").edatagrid({
            url:$.webPath() + "/jhgl/hkjh/mxList?gsId=${gsId!}&year=${year!}&month=${month!}",
            fit:true,
            singleSelect:true,
            showFooter:true,
            fitColumns:true,
            toolbar:'#hkjhmx-toolbar',
            columns:[
                [
                    {field:'khid', title:'回款客户', rowspan:2, align:'center', width:120, editor:{
                        type:'combobox', options:{
                            data:khdatas,
                            valueField:'id',
                            textField:'jc',
                            required:true}
                    }, formatter:function (value) {
                        for (var i = 0; i < khdatas.length; i++) {
                            if (khdatas[i].id == value) return khdatas[i].jc;
                        }
                        return value;
                    }},
                    {title:'应回款(元)', colspan:3, align:'center', width:150},
                    {title:'计划回款(万)', colspan:3, align:'center', width:150},
                    {field:'bz', title:'备注', width:80, align:'center', rowspan:2, editor:{type:'text'}}
                ],
                [
                    {field:'wq', title:'上期', width:50, align:'right'},
                    {field:'bq', title:'本期', width:50, align:'right'},
                    {field:'yskxj', title:'小计', width:50, align:'right'},
                    {field:'xh', title:'现汇', width:50, align:'right', editor:{type:'numberbox', options:{precision:4, required:true}}},
                    {field:'cd', title:'承兑', width:50, align:'right', editor:{type:'numberbox', options:{precision:4, required:true}}},
                    {field:'xj', title:'小计', width:50, align:'right', editor:{type:'numberbox', options:{precision:4, disabled:true}}}
                ]
            ],
            onSelect:function (rowIndex, rowData) {
//                $("#hkjhmx table[name='datagrid']").edatagrid('saveRow');
                //获取每个单元格
                $("#hkjhmx table[name='datagrid']").edatagrid('editRow', rowIndex);
                var editors = $("#hkjhmx table[name='datagrid']").datagrid('getEditors', rowIndex);
                if (editors.length == 0)
                    return;
                var xh = editors[1];
                var cd = editors[2];
                var xj = editors[3];

                xh.target.unbind("change").bind('change', function () {
                    var xhValue = $(this).val() || 0;
                    var cdValue = $(cd.target).numberbox('getValue') || 0;
                    var sum = parseFloat(xhValue) + parseFloat(cdValue);
                    $(xj.target).numberbox('setValue', sum);
                    tj(rowIndex, {xhValue:xhValue, cdValue:cdValue, sum:sum});
                });
                cd.target.unbind("change").bind('change', function () {
                    var xhValue = $(xh.target).numberbox('getValue') || 0;
                    var cdValue = $(this).val() || 0;
                    var sum = parseFloat(xhValue) + parseFloat(cdValue);
                    $(xj.target).numberbox('setValue', sum);
                    tj(rowIndex, {xhValue:xhValue, cdValue:cdValue, sum:sum});
                });

            },
            onBeforeEdit:function(rowIndex, rowData){
                $("#hkjhmx table[name='datagrid']").edatagrid('saveRow');
            }
        });
        var tj = function (rowIndex, obj) {
            var rows = $("#hkjhmx table[name='datagrid']").datagrid("getRows");
            if (rows) {
                var xhtj = 0, cdtj = 0, xjtj = 0;
                for (var i = 0; i < rows.length; i++) {
                    if (i == rowIndex) {
                        xhtj += parseFloat(obj.xhValue);
                        cdtj += parseFloat(obj.cdValue);
                        xjtj += parseFloat(obj.sum);
                    } else {
                        xhtj += parseFloat(rows[i].xh || 0);
                        cdtj += parseFloat(rows[i].cd || 0);
                        xjtj += parseFloat(rows[i].xj || 0);
                    }
                }
                var footer = {"kh":"合计", "xh":xhtj, "cd":cdtj, "xj":xjtj};
                $("#hkjhmx table[name='datagrid']").datagrid("reloadFooter", [footer]);
            }
        }
        $("#hkjhmx-toolbar a[name='new']").click(function () {
            var select = $("#hkjhmx table[name='datagrid']").edatagrid("getSelected");
            if(select){
                var index = $("#hkjhmx table[name='datagrid']").edatagrid("getRowIndex", $("#hkjhmx table[name='datagrid']").edatagrid("getSelected"));
                if(!$("#hkjhmx table[name='datagrid']").edatagrid("validateRow",index)){
                    $.messager.alert("警告","数据填写不完整","warning");
                    return;
                }
                $("#hkjhmx table[name='datagrid']").edatagrid('saveRow');
            }
            $("#hkjhmx table[name='datagrid']").edatagrid("insertRow", {
                index:0,
                row:{}
            });
        });
        $("#hkjhmx-toolbar a[name='del']").click(function () {
            if(!$("#hkjhmx table[name='datagrid']").edatagrid("getSelected")){
                return;
            }
            var index = $("#hkjhmx table[name='datagrid']").edatagrid("getRowIndex", $("#hkjhmx table[name='datagrid']").edatagrid("getSelected"));
            $("#hkjhmx table[name='datagrid']").edatagrid("deleteRow", index);
            tj();
        });
    });
</script>
<div style="width:100%;height:100%;" id="hkjhmx">
    <div id="hkjhmx-toolbar">
        <a href="javascript:void(0)" name="new" class="easyui-linkbutton" iconCls="icon-add" plain="true">新增</a>
        <a href="javascript:void(0)" name="del" class="easyui-linkbutton" iconCls="icon-edit" plain="true">删除</a>

        <div class="textcon">
            <form id="hkjh_save" action="${base}/jhgl/hkjh/save" method="post">
            <table class="tb tab-t t-bor">
                <col width="10%">
                <col width="20%">
                <col width="10%">
                <col width="20%">
                <col width="10%">
                <tr>
                    <th>回款月份：</th>
                    <td>
                        <input name="hkjh.nf" type="hidden" value="${year!}" />
                        <input name="hkjh.yf" type="hidden" value="${month!}" />
                        ${year!}年${month!}月
                    </td>
                    <th>公司名称：</th>
                    <td>
                        <input name="hkjh.gsid" type="hidden" value="${gsId!}"/>
                        ${gsJc!}
                    </td>
                    <th>备注：</th>
                    <td><input name="hkjh.bz" type="text" class="input-text" value="" style="width:250px;"></td>
                </tr>
            </table>
            </form>
        </div>
    </div>
    <table name="datagrid"></table>
</div>